package com.keba.lamp.complaint.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.keba.basic.base.entity.SuperEntity;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.entity.Case;
import com.keba.lamp.complaint.entity.Defendant;
import com.keba.lamp.complaint.entity.Plaintff;
import com.keba.lamp.complaint.service.CaseService;
import com.keba.lamp.complaint.service.DefendantService;
import com.keba.lamp.complaint.service.PlaintffService;
import com.keba.lamp.complaint.vo.result.*;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.file.service.FileService;
import com.keba.lamp.model.vo.save.AppendixSaveVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.keba.lamp.complaint.service.OnlineCaseFilingService;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.lamp.complaint.manager.OnlineCaseFilingManager;
import com.keba.lamp.complaint.entity.OnlineCaseFiling;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 业务实现类
 * 在线立案
 * </p>
 *
 * @author twg
 * @date 2025-07-27 20:56:45
 * @create [2025-07-27 20:56:45] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OnlineCaseFilingServiceImpl extends SuperServiceImpl<OnlineCaseFilingManager, Long, OnlineCaseFiling> implements OnlineCaseFilingService {

    private final CaseService caseService;
    private final PlaintffService plaintffService;
    private final DefendantService defendantService;
    private final FileService fileService;
    private final AppendixService appendixService;

    @Override
    public List<OnlineCaseResultVO> listByCaseName(String caseName) {
        List<Case> cases = caseService.list(Wrappers.<Case>lambdaQuery().like(Case::getCaseName, caseName));
        if (CollectionUtil.isEmpty(cases)) {
            return Collections.emptyList();
        }
        List<Long> caseIds = cases.stream().map(SuperEntity::getId).toList();
        List<OnlineCaseFiling> onlineCaseFilings = list(Wrappers.<OnlineCaseFiling>lambdaQuery().in(OnlineCaseFiling::getCaseId, caseIds));
        if (CollectionUtil.isEmpty(onlineCaseFilings)) {
            List<OnlineCaseResultVO> list = BeanPlusUtil.toBeanList(cases, OnlineCaseResultVO.class);
            buildPlaintffAndDefendant(list, caseIds);
            return list;
        }
        List<Case> noneMatch = cases.stream().filter(o -> onlineCaseFilings.stream().noneMatch(onlineCaseFiling -> onlineCaseFiling.getCaseId().equals(o.getId()))).toList();
        List<OnlineCaseResultVO> list = BeanPlusUtil.toBeanList(noneMatch, OnlineCaseResultVO.class);
        buildPlaintffAndDefendant(list, caseIds);
        return list;
    }


    @Override
    public OnlineCaseResultVO detail(Long id) {
        OnlineCaseFiling onlineCaseFiling = getById(id);
        Case aCase = caseService.getById(onlineCaseFiling.getCaseId());
        return null;
    }

    @Override
    protected <SaveVO> void saveAfter(SaveVO saveVO, OnlineCaseFiling entity) {
        super.saveAfter(saveVO, entity);
        // 保存图片
//        appendixService.save(AppendixSaveVO.build(entity.getId(), AppendixType.Complaint.COMPLAINT__CHANNEL__CUSTOMER__IMAGE, saveVO.getAppendixIcon()));
    }

    private void buildPlaintffAndDefendant(List<OnlineCaseResultVO> list, List<Long> caseIds) {
        Map<Long, List<Plaintff>> plaintffMap = plaintffService.list(Wrappers.<Plaintff>lambdaQuery().in(Plaintff::getCaseId, caseIds)).stream().collect(Collectors.groupingBy(Plaintff::getCaseId));
        Map<Long, List<Defendant>> defendantMap = defendantService.list(Wrappers.<Defendant>lambdaQuery().in(Defendant::getCaseId, caseIds)).stream().collect(Collectors.groupingBy(Defendant::getCaseId));
        list.forEach(o -> {
            o.setPlaintffResultVOList(BeanPlusUtil.toBeanList(plaintffMap.getOrDefault(o.getId(), new ArrayList<>()), PlaintffResultVO.class));
            o.setDefendantResultVOList(BeanPlusUtil.toBeanList(defendantMap.getOrDefault(o.getId(), new ArrayList<>()), DefendantResultVO.class));
            List<PlaintffResultVO> plaintffResultVOS = plaintffService.getResultByCaseId(o.getId());
            List<DefendantResultVO> defendantResultVOS = defendantService.getResultByCaseId(o.getId());
            List<CaseMaterialResultVO> plaintffMaterials = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(plaintffResultVOS)) {
                plaintffResultVOS.forEach(p -> plaintffMaterials.addAll(BeanPlusUtil.toBeanList(p.getFileList(), CaseMaterialResultVO.class)));
            }
            o.setPlaintffMaterials(plaintffMaterials);
            List<CaseMaterialResultVO> defendantMaterials = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(defendantResultVOS)) {
                defendantResultVOS.forEach(d -> defendantMaterials.addAll(BeanPlusUtil.toBeanList(d.getFileList(), CaseMaterialResultVO.class)));
            }
            o.setDefendantMaterials(defendantMaterials);
            List<EvidenceResultVO> evidenceResultVOList = caseService.getEvidenceList(o.getId(), o.getBizType(), o.getCaseType());
            o.setEvidenceResultVOList(evidenceResultVOList);
        });
    }


}


